package com.keda.dao;

import com.keda.model.SmallType;
import com.keda.util.DBUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SmallTypeDao {

    public List<SmallType> queryByBigType(Integer bigTypeId) {

        // 建立连接
        Connection connection = DBUtil.createConnection();

        // 查询数据
        // 注意：参数用问号（?）代替，尽量不要拼接，避免SQL注入
        String sql = "select id, name, bigTypeId from t_smalltype where bigTypeId = ?";

        QueryRunner queryRunner = new QueryRunner();
        try {
            List<SmallType> smallTypes = queryRunner.query(connection, sql,
                    new BeanListHandler<SmallType>(SmallType.class), bigTypeId);
            return smallTypes;
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        } finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
